package com.atguigu.netty.group.chat.server;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * 群聊服务中心
 */
@Slf4j
public class GroupChatServer {

    public static void main(String[] args) throws InterruptedException {
        // 1、 创建线程组
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        // 2. 创建服务端的启动对象
        ServerBootstrap bootstrap = new ServerBootstrap();
        // 配置
        bootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new GroupChatServerHandler());
                    }
                });
        log.info("服务器 is ready。。。");
        ChannelFuture cf = bootstrap.bind(8848).sync();
        cf.channel().closeFuture().sync();
    }
}
